package com.ciko.code.logic.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.ciko.code.logic.bean.ContactSuperBean;
import com.ciko.code.logic.db.NewContactsDBHelper;
import com.ciko.code.logic.table.ContactGroupTable;
import com.ciko.code.logic.table.ContactsInfoTable;

import java.util.ArrayList;
import java.util.List;


/**
 * 联系人分组 数据操作类
 */
public class ContactGroupDao {
    private NewContactsDBHelper dbHelper;

    private ContactGroupDao(Context context) {
        dbHelper = NewContactsDBHelper.getInstance(context);
    }

    public static ContactGroupDao getInstance(Context context) {
        return new ContactGroupDao(context);
    }

    private ContentValues getContentValues(ContactSuperBean bean, boolean isInsert) {
        final ContentValues values = new ContentValues();
        if (isInsert) {
            values.put(ContactGroupTable.SuperColums.GROUP_ID, bean.getGroupId());
        }
        values.put(ContactGroupTable.SuperColums.NAME, bean.getName());
        values.put(ContactGroupTable.SuperColums.SORT_NUM, bean.getSortNum());
        values.put(ContactGroupTable.SuperColums.IS_DELETE, bean.getIsDelete());
        values.put(ContactGroupTable.SuperColums.COMPANY_ID, bean.getCompanyId());
        return values;
    }

    public boolean add(ContactSuperBean bean) {
        try {
            dbHelper.getSqliteDatabaseW().insert(ContactGroupTable.table, null, getContentValues(bean, true));
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dbHelper.closeSqliteDatabaseW();
        }
        return false;
    }

    public boolean update(ContactSuperBean bean) {
        final String selection = ContactGroupTable.SuperColums.GROUP_ID + " = ? ";
        final String[] args = new String[]{
                String.valueOf(bean.getGroupId())
        };
        int v = 0;
        try {
            v = dbHelper.getSqliteDatabaseW().update(ContactGroupTable.table, getContentValues(bean, false), selection, args);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dbHelper.closeSqliteDatabaseW();
        }
        return v > 0;
    }

    public boolean delete(int groupId) {
        final String selection = ContactGroupTable.SuperColums.GROUP_ID + " = ? ";
        final String[] args = new String[]{
                String.valueOf(groupId)
        };
        int v = dbHelper.getSqliteDatabaseW().delete(ContactGroupTable.table, selection, args);
        dbHelper.closeSqliteDatabaseW();
        return v > 0;
    }

    public boolean deleteAll() {
        int v = dbHelper.getSqliteDatabaseW().delete(ContactGroupTable.table, null, null);
        dbHelper.closeSqliteDatabaseW();
        return v > 0;
    }

    public ContactSuperBean getById(int groupId) {
        ContactSuperBean bean = null;
        final String selection = ContactGroupTable.SuperColums.GROUP_ID + " = ? ";
        final String[] args = new String[]{
                String.valueOf(groupId)
        };
        Cursor c = dbHelper.getSqliteDatabaseR().query(ContactGroupTable.table, null, selection, args, null, null, null);
        try {
            if (c != null && c.getCount() > 0) {
                c.moveToFirst();
                bean = new ContactSuperBean(c.getInt(c.getColumnIndex(ContactGroupTable.SuperColums.GROUP_ID)),
                        c.getString(c.getColumnIndex(ContactGroupTable.SuperColums.IS_DELETE)),
                        c.getString(c.getColumnIndex(ContactGroupTable.SuperColums.NAME)),
                        c.getInt(c.getColumnIndex(ContactGroupTable.SuperColums.SORT_NUM)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dbHelper.closeCursor(c);
            dbHelper.closeSqliteDatabaseR();
        }

        return bean;
    }

    public List<ContactSuperBean> getAll() {
        List<ContactSuperBean> list = null;
        Cursor c = dbHelper.getSqliteDatabaseR().query(ContactGroupTable.table, null, null, null, null, null, null);
        try {
            if (c != null && c.getCount() > 0) {
                list = new ArrayList<>();
                while (c.moveToNext()) {
                    list.add(new ContactSuperBean(c.getInt(c.getColumnIndex(ContactGroupTable.SuperColums.GROUP_ID)),
                            c.getString(c.getColumnIndex(ContactGroupTable.SuperColums.IS_DELETE)),
                            c.getString(c.getColumnIndex(ContactGroupTable.SuperColums.NAME)),
                            c.getInt(c.getColumnIndex(ContactGroupTable.SuperColums.SORT_NUM))));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dbHelper.closeCursor(c);
            dbHelper.closeSqliteDatabaseR();
        }

        return list;
    }
    public List<ContactSuperBean> getAll(int companyId) {
        List<ContactSuperBean> list = null;
        final String[] args = new String[]{
                String.valueOf(companyId)
        };
        final String selection = ContactsInfoTable.AddressBookInfoColums.COMPANY_ID + " = ? ";
        Cursor c = dbHelper.getSqliteDatabaseR().query(ContactGroupTable.table, null, selection, args, null, null, null);
        try {
            if (c != null && c.getCount() > 0) {
                list = new ArrayList<>();
                while (c.moveToNext()) {
                    list.add(new ContactSuperBean(c.getInt(c.getColumnIndex(ContactGroupTable.SuperColums.GROUP_ID)),
                            c.getString(c.getColumnIndex(ContactGroupTable.SuperColums.IS_DELETE)),
                            c.getString(c.getColumnIndex(ContactGroupTable.SuperColums.NAME)),
                            c.getInt(c.getColumnIndex(ContactGroupTable.SuperColums.SORT_NUM))));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dbHelper.closeCursor(c);
            dbHelper.closeSqliteDatabaseR();
        }

        return list;
    }
}
